package servlets;



import bean.Goods;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import dao.GoodsDao;
import dao.GoodsJDBCDao;
import utils.Response;
import utils.ResponseCode;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class GetAllGoodsesServlet  extends HttpServlet  {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        该方法的作用是向用户返回所有的商品数据
        resp.setContentType("application/json;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
//        第一步：获取所有商品
        List<Goods> goodsList=null;
        try{
            goodsList=GoodsDao.findAll();
        }catch (SQLException e){
            e.printStackTrace();
        }

        Response<List<Goods>> build = Response.build(ResponseCode.SUCCESS, goodsList);

        resp.getWriter().write(JSON.toJSON(build).toString());

      
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
